package com.module.product.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import base.dao.GenericHibernateDao;

import com.module.product.vo.AttrValue;


public class AttrValueDao extends GenericHibernateDao<AttrValue, String> {
    private static final AttrValueDao instance = new AttrValueDao();

    private AttrValueDao() {
    }

    public synchronized static AttrValueDao getInstance() {
        return instance;
    }
    
    public boolean delAllAttrVbyPidAndGid(Connection conn,String productId,String groupId){
        boolean b = false;
        try
        {
            String sql = " delete from  p_attr_value  where  c_product_id =? and c_attrgroup_id =?";
            PreparedStatement stmt = conn.prepareStatement(sql);
                              stmt.setString(1,productId);
                              stmt.setString(2,groupId);
            stmt.executeUpdate();
            b = true ;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        } 
        return b;
    }
    
    
	public boolean delAllAttrVbyPid(Connection conn,String productId) {
		boolean b = false;
		PreparedStatement stmt = null;
		try {
			String sql = "delete from  p_attr_value  where  c_product_id =? ";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, productId);
			stmt.executeUpdate();
			b = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			this.close(stmt);
		}
		return b;
	}
    
    public List getattvbyproductid(Session session,String productId){
        List<AttrValue> list = null;
        Transaction tx = null;

        String hql = "from AttrValue where  productId=?";
        Query query = session.createQuery(hql);
        query.setString(0, productId);
        list = query.list();
        return list;
    }
    
}
